﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmApiLibrary.datatypes;
using SmApiLibrary.helps;

namespace SmApiLibrary.libs
{
    public class WeiBoQQ : IWeiBo
    {
        #region 时间线相关
        /// <summary>
        /// 获得大厅时间线(第1页/20条微博)
        /// </summary>
        /// <returns></returns>
        public string BroadcastTimeline()
        {
            return BroadcastTimeline(0, 20, 0);
        }

        private string BroadcastTimeline(int pageflag, int reqnum, int pagetime)
        {
            string parameters = string.Format("?format={0}&pageflag={1}&reqnum={2}&pagetime={3}", Format, pageflag, reqnum, pagetime);
            string url = "http://open.t.qq.com/api/statuses/broadcast_timeline" + parameters;
            string response = OAuth.OAuthWebRequest(Method.GET, url, null);
            return response;
        }

        /// <summary>
        /// 获得用户时间线
        /// </summary>
        /// <returns></returns>
       
        #endregion

        #region 微博相关
        /// <summary>
        /// 发布一条微博(不带图片)
        /// <param name="content">微博文字</param>
        /// <returns></returns>
        /// </summary>
        public string AddWeiBo(string content)
        {
            string url = "http://open.t.qq.com/api/t/add";
            string postData = "format=" + Format + "&content=" + Utility.UrlEncode(content);// +"&clientip=127.0.0.1" + "&jing=&wei=";
            string response = OAuth.OAuthWebRequest(Method.POST, url, postData);
            return response;
        }

        /// <summary>
        /// 发布一条微博(图片)
        /// </summary>
        /// <param name="content">微博文字</param>
        /// <param name="picturefilename">图片文件名</param>
        /// <returns></returns>
        public string AddWeiBoPic(string content, string picturefilename)
        {
            string url = "http://open.t.qq.com/api/t/add_pic";
            string postData = "format=" + Format + "&content=" + Utility.UrlEncode(content) + "&jing="; // +"&filename=" + picturefilename;
            string response = OAuth.OAuthWebRequest(Method.POST, url, postData, picturefilename);
            return response;
        }

        public string GetUserId()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region 账户相关
        /// <summary>
        /// 获取自己的用户信息
        /// </summary>
        /// <returns></returns>
        public string GetWeiBoUserInfo()
        {
            string url = "http://open.t.qq.com/api/user/info?format=json";
            string result = OAuth.OAuthWebRequest(Method.GET, url, null);

            return result;
        }
        private QQUser GetSinaUser(string result)
        {
            QQUser quer = JsonHelper.JsonDeserialize<QQUser>(result);
            return quer;
        }

        #endregion

        #region 关系链相关
        /// <summary>
        /// 获得关注用户(1页/30个)
        /// </summary>
        /// <returns></returns>
       
        #endregion

        #region 私信相关
        #endregion

        #region 搜索相关
        #endregion

        #region 热度,趋势
        #endregion

        #region 数据更新相关
        #endregion

        #region 数据收藏
        #endregion

        #region 话题相关
        #endregion

        #region 其他
        #endregion


        /// <summary>
        /// QQ的OAuth对象
        /// </summary>
        public OAuthQQ OAuth { get; set; }

        /// <summary>
        /// 创建QQ微博
        /// </summary>
        public WeiBoQQ()
        {
            if (OAuth == null)
            {
                OAuth = new OAuthQQ();
            }
        }
        private const string Format = "json";

        private static string ConvertTwitterDate(string timestamp)
        {
            var dtbase = new DateTime(1970, 1, 1, 8, 0, 0, 0); // UTC +8
            dtbase = dtbase.AddSeconds(double.Parse(timestamp));
            string dayElement = dtbase.Date == DateTime.Now.Date ? "Today" : dtbase.Day.ToString();
            string timeElement = string.Format("{0:D2}:{1:D2}", dtbase.TimeOfDay.Hours, dtbase.TimeOfDay.Minutes);
            return string.Concat(dayElement, " ", timeElement);
        }

        public void SetOAuth(string token, string secret)
        {
            OAuth.Token = token;
            OAuth.TokenSecret = secret;
        }

        #region IWeiBo Members

        public WeiBoType UserType
        {
            get { return WeiBoType.QQ; }
        }

        public OAuthBase GetOAuth()
        {
            return OAuth;
        }

        /// <summary>
        /// OAuth是否已经正确授权
        /// </summary>
        public bool IsEnabled
        {
            get
            {
                return !string.IsNullOrEmpty(OAuth.Token);
            }
        }
        #endregion
    }
}
